//
//  CZTableViewCell+JC.swift
//  GZWeibo007
//
//  Created by mac on 15/12/24.
//  Copyright © 2015年 Apple. All rights reserved.
//

import UIKit
import SDWebImage

class CZTableViewCell_JC: UITableViewCell {
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        // 准备UI
        backgroundColor = UIColor(white: 0.95, alpha: 1)
        
        preapreUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var comments:CZcommentModel? {
        didSet {
            
            if comments == nil {
                return
            }
            HPnameLabel.text = comments!.user?.screen_name
            HPtimeLabel.text = comments!.created_at
            HPcontentLabel.text = comments!.text
            HPiconView.sd_setImageWithURL(NSURL(string: comments!.user!.profile_image_url!), placeholderImage: nil, options: SDWebImageOptions.LowPriority)
            
        }
        
        
    }
    
    //评论实现UI
    private  func preapreUI() {
        //--1:分割线UI
        contentView.addSubview(HPseparatorView)
        //--1:分割线UI
//        addSubview(HPseparatorView2)
        //--2:用户图像UI
        contentView.addSubview(HPiconView)
        
        //--3:用户名称UI
        contentView.addSubview(HPnameLabel)
        
        //--4:时间UI
        contentView.addSubview(HPtimeLabel)
        
        //--4:内容UI
        contentView.addSubview(HPcontentLabel)
        
        //--5:点赞按扭UI
        contentView.addSubview(HPlikeButton)
        
        updateConstraints()
        
    }
    
    override func updateConstraints() {
        
        HPseparatorView.snp_remakeConstraints { (make) -> Void in
            make.top.leading.width.equalTo(contentView)
            make.height.equalTo(1)
        }
        
        HPiconView.snp_remakeConstraints { (make) -> Void in
            make.top.leading.equalTo(HPseparatorView).offset(8)
            
            make.width.height.equalTo(40)
        }
        
        HPnameLabel.snp_remakeConstraints { (make) -> Void in
            make.top.equalTo(HPiconView)
            make.leading.equalTo(HPiconView.snp_trailing).offset(3)
        }
        
        HPtimeLabel.snp_remakeConstraints { (make) -> Void in
            make.leading.trailing.equalTo(HPnameLabel)
            make.top.equalTo(HPnameLabel.snp_bottom).offset(3)
        }
        
        HPcontentLabel.snp_remakeConstraints { (make) -> Void in
            make.leading.equalTo(HPtimeLabel)
            make.top.equalTo(HPtimeLabel.snp_bottom).offset(3)
            make.trailing.equalTo(contentView).offset(-20)
            make.bottom.equalTo(contentView)
//            make.height.equalTo(100)
            
        }
        
        HPlikeButton.snp_remakeConstraints { (make) -> Void in
            make.top.equalTo(contentView).offset(8)
            make.trailing.equalTo(contentView).offset(-8)
        }
        
        super.updateConstraints()
    }
    
    func likeButtonClick()
    {
//        if HPlikeButton.selected {
//            HPlikeButton.selected=false
//        }
//        else {
//            HPlikeButton.selected=true
//        }
        HPlikeButton.selected = !HPlikeButton.selected
    }
    // MARK: - 点赞Cell懒加载
    //--1:分割线UI
    private lazy var HPseparatorView:UIView = {
        
        let separatorView = UIView()
        separatorView.backgroundColor = UIColor.lightGrayColor()
        
        return separatorView
    }()
    //--2:用户图像UI
    private lazy var HPiconView:UIImageView = {
        
        let iconView=UIImageView(image: UIImage(named: "avatar"))
        
        iconView.layer.cornerRadius=5
        iconView.layer.masksToBounds=true
        
        return iconView
    }()
    
    //--3:用户名称UI
    private lazy var HPnameLabel:UILabel = {
        
        let  nameLabel = UILabel(color: UIColor.blackColor(), fontSize: 14);
        nameLabel.sizeToFit()
        return nameLabel
    }()
    
    //--4:时间UI
    private lazy var HPtimeLabel:UILabel = {
        
        let timeLabel = UILabel(color: UIColor.orangeColor(), fontSize: 12)
        timeLabel.sizeToFit()
        return timeLabel
    }()
    
    //--4:内容UI
    private lazy var HPcontentLabel:UILabel = {
        
        let contentLabel = UILabel(color: UIColor.blackColor(), fontSize: 14)
        contentLabel.numberOfLines = 0
        contentLabel.sizeToFit()
        return contentLabel
    }()
    
    //--5:点赞按扭UI
    private lazy var HPlikeButton:UIButton = {
        let likebutton=UIButton()
        likebutton.setBackgroundImage(UIImage(named: "timeline_icon_unlike"), forState: UIControlState.Normal)
        likebutton.setBackgroundImage(UIImage(named: "timeline_icon_like"), forState: UIControlState.Selected)
        likebutton.addTarget(self, action: "likeButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        likebutton.sizeToFit()
        return likebutton
    }()
}
